package com.wxb.controller;

import com.github.pagehelper.PageHelper;
import com.wxb.common.core.web.domain.AjaxResult;
import com.wxb.common.security.utils.SecurityUtils;
import com.wxb.domain.*;
import com.wxb.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Date 2025-05-30 10:36
 * @Version 1.0
 * @Description
 * @ClassName MallIndexController
 **/
@RestController
@RequestMapping("/index")
public class MallIndexController {
    @Autowired
    private IMallAdService adService;
    @Autowired
    private IMallChannelService channelService;
    @Autowired
    private IMallGoodsService goodsService;
    @Autowired
    private IMallBrandService brandService;
    @Autowired
    private IMallTopicService topicService;
    @Autowired
    private IMallCategoryService categoryService;
    @Autowired
    private IMallCartService cartService;

    @PostMapping(value = "test")
    public Object test() {
        return AjaxResult.success("请求成功yyy");
    }

    /**
     * app首页
     */
    @PostMapping(value = "index")
    public Object index() {
        Map<String, Object> resultObj = new HashMap<String, Object>();
        //
        Map<String, Object> param = new HashMap<String, Object>();
        param.put("adPositionId", 1);
        List<MallAdEntity> banner = adService.queryList(param);
        resultObj.put("banner", banner);
        //
        param = new HashMap<String, Object>();
        param.put("sidx", "sort_order ");
        param.put("order", "asc ");
        List<MallChannelEntity> channel = channelService.queryList(param);
        resultObj.put("channel", channel);
        //
        param = new HashMap<String, Object>();
        param.put("isNew", 1);
        param.put("isDelete", 0);
        param.put("fields", "id, name, list_pic_url, retail_price");
        PageHelper.startPage(0, 4, false);
        List<MallGoodsEntity> newGoods = goodsService.queryList(param);
        resultObj.put("newGoodsList", newGoods);
        //
        param = new HashMap<String, Object>();
        param.put("isHot", "1");
        param.put("isDelete", 0);
        PageHelper.startPage(0, 3, false);
        List<MallGoodsEntity> hotGoods = goodsService.queryHotGoodsList(param);
        resultObj.put("hotGoodsList", hotGoods);
        // 当前购物车中
        List<MallCartEntity> cartList = new ArrayList<MallCartEntity>();
        if (null != SecurityUtils.getUserId()) {
            //查询列表数据
            Map<String, Object> cartParam = new HashMap<String, Object>();
            cartParam.put("userId", SecurityUtils.getUserId());
            cartList = cartService.queryList(cartParam);
        }
        if (null != cartList && cartList.size() > 0 && null != hotGoods && hotGoods.size() > 0) {
            for (MallGoodsEntity goodsVo : hotGoods) {
                for (MallCartEntity cartVo : cartList) {
                    if (goodsVo.getId().equals(cartVo.getGoodsId())) {
                        goodsVo.setCartNum(cartVo.getNumber());
                    }
                }
            }
        }
        //
        param = new HashMap<String, Object>();
        param.put("isNew", 1);
        param.put("sidx", "new_sort_order ");
        param.put("order", "asc ");
        param.put("offset", 0);
        param.put("limit", 4);
        List<MallBrandEntity> brandList = brandService.queryList(param);
        resultObj.put("brandList", brandList);

        param = new HashMap<String, Object>();
        param.put("offset", 0);
        param.put("limit", 3);
        List<MallTopicEntity> topicList = topicService.queryList(new MallTopicEntity());
        resultObj.put("topicList", topicList);

        param = new HashMap<String, Object>();
        param.put("parentId", 0);
        param.put("notName", "推荐");//<>
        List<MallCategoryEntity> categoryList = categoryService.queryList(param);
        List<Map<String, Object>> newCategoryList = new ArrayList<>();

        for (MallCategoryEntity categoryItem : categoryList) {
            param.remove("fields");
            param.put("parentId", categoryItem.getId());
            List<MallCategoryEntity> categoryEntityList = categoryService.queryList(param);
            List<Integer> childCategoryIds = new ArrayList<>();
            for (MallCategoryEntity categoryEntity : categoryEntityList) {
                childCategoryIds.add(categoryEntity.getId());
            }
            //
            param = new HashMap<String, Object>();
            param.put("categoryIds", childCategoryIds);
            param.put("fields", "id as id, name, list_pic_url, retail_price as retail_price");
            PageHelper.startPage(0, 7, false);
            List<MallGoodsEntity> categoryGoods = goodsService.queryList(param);
            Map<String, Object> newCategory = new HashMap<String, Object>();
            newCategory.put("id", categoryItem.getId());
            newCategory.put("name", categoryItem.getName());
            newCategory.put("goodsList", categoryGoods);
            newCategoryList.add(newCategory);
        }
        resultObj.put("categoryList", newCategoryList);
        return AjaxResult.success(resultObj);
    }


    /**
     * app首页
     */
    @PostMapping(value = "newGoods")
    public Object newGoods() {
        Map<String, Object> resultObj = new HashMap<String, Object>();
        //
        Map<String, Object> param = new HashMap<String, Object>();
        param.put("isNew", 1);
        param.put("isDelete", 0);
        param.put("fields", "id, name, list_pic_url, retail_price");
        PageHelper.startPage(0, 4, false);
        List<MallGoodsEntity> newGoods = goodsService.queryList(param);
        resultObj.put("newGoodsList", newGoods);
        //

        return AjaxResult.success(resultObj);
    }

    @PostMapping(value = "hotGoods")
    public Object hotGoods() {
        Map<String, Object> resultObj = new HashMap<String, Object>();
        //
        Map<String, Object> param = new HashMap<String, Object>();
        param.put("isHot", "1");
        param.put("isDelete", 0);
        PageHelper.startPage(0, 3, false);
        List<MallGoodsEntity> hotGoods = goodsService.queryHotGoodsList(param);
        resultObj.put("hotGoodsList", hotGoods);

        return AjaxResult.success(resultObj);
    }

    @PostMapping(value = "topic")
    public Object topic() {
        Map<String, Object> resultObj = new HashMap<String, Object>();
        //
        Map<String, Object> param = new HashMap<String, Object>();
        param.put("offset", 0);
        param.put("limit", 3);
        List<MallTopicEntity> topicList = topicService.queryList(new MallTopicEntity());
        resultObj.put("topicList", topicList);
        //

        return AjaxResult.success(resultObj);
    }

    @PostMapping(value = "brand")
    public Object brand() {
        Map<String, Object> resultObj = new HashMap<String, Object>();
        //
        Map<String, Object> param = new HashMap<String, Object>();
        param.put("isNew", 1);
        param.put("sidx", "new_sort_order ");
        param.put("order", "asc ");
        param.put("offset", 0);
        param.put("limit", 4);
        List<MallBrandEntity> brandList = brandService.queryList(param);
        resultObj.put("brandList", brandList);
        //

        return AjaxResult.success(resultObj);
    }

    @PostMapping(value = "category")
    public Object category() {
        Map<String, Object> resultObj = new HashMap<String, Object>();
        //
        Map<String, Object> param = new HashMap<String, Object>();
        param = new HashMap<String, Object>();
        param.put("parentId", 0);
        param.put("notName", "推荐");//<>
        List<MallCategoryEntity> categoryList = categoryService.queryList(param);
        List<Map<String, Object>> newCategoryList = new ArrayList<>();

        for (MallCategoryEntity categoryItem : categoryList) {
            param.remove("fields");
            param.put("parentId", categoryItem.getId());
            List<MallCategoryEntity> categoryEntityList = categoryService.queryList(param);
            List<Integer> childCategoryIds = null;
            if (categoryEntityList != null && categoryEntityList.size() > 0) {
                childCategoryIds = new ArrayList<>();
                for (MallCategoryEntity categoryEntity : categoryEntityList) {
                    childCategoryIds.add(categoryEntity.getId());
                }
            }
            //
            param = new HashMap<String, Object>();
            param.put("categoryIds", childCategoryIds);
            param.put("fields", "id as id, name, list_pic_url, retail_price as retail_price");
            param.put("isDelete", "0");
            PageHelper.startPage(0, 7, false);
            List<MallGoodsEntity> categoryGoods = goodsService.queryList(param);
            Map<String, Object> newCategory = new HashMap<String, Object>();
            newCategory.put("id", categoryItem.getId());
            newCategory.put("name", categoryItem.getName());
            newCategory.put("goodsList", categoryGoods);
            newCategoryList.add(newCategory);
        }
        resultObj.put("categoryList", newCategoryList);
        //

        return AjaxResult.success(resultObj);
    }

    @PostMapping(value = "banner")
    public Object banner() {
        Map<String, Object> resultObj = new HashMap<String, Object>();
        //
        Map<String, Object> param = new HashMap<String, Object>();
        param.put("adPositionId", 1);
        List<MallAdEntity> banner = adService.queryList(param);
        resultObj.put("banner", banner);
        //

        return AjaxResult.success(resultObj);
    }

    @PostMapping(value = "channel")
    public Object channel() {
        Map<String, Object> resultObj = new HashMap<String, Object>();
        //
        Map<String, Object> param = new HashMap<String, Object>();
        param = new HashMap<String, Object>();
        param.put("sidx", "sort_order ");
        param.put("order", "asc ");
        List<MallChannelEntity> channel = channelService.queryList(param);
        resultObj.put("channel", channel);
        //

        return AjaxResult.success(resultObj);
    }
}
